#!/bin/bash


if [ "$*" = "" ]
then
   echo "Usage: applyUpdates [cdrom | disk]"
   exit 0
fi
cv=`/opt/hsc/bin/hsc version | grep Release | cut -d':' -f 2`
case $1 in

cdrom )
   mount /mnt/cdrom
   if [ $? -ne 0 ]
   then
     echo "Unable to mount media" 1>&2
     exit 1
   fi
   if [ ! -f /mnt/cdrom/.signature ]
   then
     echo "Incorrect media inserted" 1>&2
     umount /mnt/cdrom
     exit 2
   else
     x=`cat /mnt/cdrom/.signature`
     fv=`echo $x | cut -d':' -f 1`
     fr=`echo $x | cut -d':' -f 2`
     if [ "$fv" = "HSC Update Software" -a "$fr" = "$cv" ]
     then
        if [ ! -x /mnt/cdrom/applyHSCUpdates ]
        then
           echo "Incorrect media inserted" 1>&2
           umount /mnt/cdrom
           exit 1
	else
           /mnt/cdrom/applyHSCUpdates
        fi
     else
        echo "Incorrect media inserted" 1>&2
        umount /mnt/cdrom
        exit 2
     fi
   fi
   
   umount /mnt/cdrom
   ;;
disk )
   if [ -f /usr/local/hsc_install.images/*.zip ]
   then
      /usr/bin/unzip -o /usr/local/hsc_install.images/*.zip -d /usr/local/hsc_install.images
      if [ ! -f /usr/local/hsc_install.images/.signature ]
      then
         echo "No update software found on disk" 1>&2
         rm -rf /usr/local/hsc_install.images/*
         rm -rf /usr/local/hsc_install.images/.signature
         exit 3
      else
         x=`cat /usr/local/hsc_install.images/.signature`
         fv=`echo $x | cut -d':' -f 1`
         fr=`echo $x | cut -d':' -f 2`
         if [ "$fv" = "HSC Update Software" -a "$fr" = "$cv" ]
         then
            if [ ! -x /usr/local/hsc_install.images/applyHSCUpdates ]
            then
              echo "No update software found on disk" 1>&2
              rm -rf /usr/local/hsc_install.images/*
              rm -rf /usr/local/hsc_install.images/.signature
              exit 3
	    else
              /usr/local/hsc_install.images/applyHSCUpdates
	      if [ $? -eq 0 ]
              then
                 rm -rf /usr/local/hsc_install.images/*
		 rm -rf /usr/local/hsc_install.images/.signature
              fi
            fi
         else
            echo "No update software found on disk" 1>&2
            rm -rf /usr/local/hsc_install.images/*
            rm -rf /usr/local/hsc_install.images/.signature
            exit 3
         fi
      fi
   fi
   ;;
esac
exit 0

